﻿Public Class frmCylinderGauge
    Private _MoveControl As PolyMonControls.MoveControl
    Private _SizeControl As PolyMonControls.SizeControl

    Private Sub frmCylinderGauge_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cboLabelAlignment.DataSource = System.Enum.GetValues(GetType(PolyMonControls.CylinderGauge.LabelAlignment))

        cboLabelAlignment.DataBindings.Add("SelectedItem", CylinderGauge1, "LabelAlign")
        chkDisplayLabels.DataBindings.Add("Checked", CylinderGauge1, "DisplayLabels")
        pbEmptyColor.DataBindings.Add("BackColor", CylinderGauge1, "EmptyColor")
        pbFillColor.DataBindings.Add("BackColor", CylinderGauge1, "FillColor")
        nudGaugeValue.DataBindings.Add("Value", CylinderGauge1, "FillPercentage")

        AddHandler pbEmptyColor.Click, AddressOf ChooseColor
        AddHandler pbFillColor.Click, AddressOf ChooseColor

        AddHandler cboLabelAlignment.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler chkDisplayLabels.CheckedChanged, AddressOf ControlValueChanged
        AddHandler nudGaugeValue.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbEmptyColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbFillColor.BackColorChanged, AddressOf ControlValueChanged

        'Add support for run-time moving/sizing cylinder gauge
        _MoveControl = New PolyMonControls.MoveControl(pnlGaugeContainer)
        _SizeControl = New PolyMonControls.SizeControl(pnlGaugeContainer)
    End Sub


    Private Sub ChooseColor(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim pb As PictureBox = CType(sender, PictureBox)
        With Me.ColorDialog1
            .Color = pb.BackColor
            Dim res As DialogResult = .ShowDialog()

            If res = Windows.Forms.DialogResult.OK Then
                pb.BackColor = .Color
            End If
        End With
    End Sub

    Private Sub ControlValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Ctl As Control = CType(sender, Control)
        If Ctl.DataBindings.Count > 0 Then
            Ctl.DataBindings(0).WriteValue()
        End If
    End Sub

    Private Sub CylinderGauge1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CylinderGauge1.Click
        'Turn on Sizing/Moving of CylinderGauge1
        _MoveControl.BoundControl = CylinderGauge1
        _SizeControl.BoundControl = CylinderGauge1
    End Sub

    Private Sub pnlGaugeContainer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnlGaugeContainer.Click
        'Turn off Sizing/Moving of CylinderGauge1
        _MoveControl.BoundControl = Nothing
        _SizeControl.BoundControl = Nothing
    End Sub


End Class